package com.ace3l.legal.gwt.lawfirmportal.createproject.client;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.synesthesiaconsulting.gwt.resources.client.DirectorUnit;
import com.synesthesiaconsulting.gwt.resources.client.RichTextField;
import com.synesthesiaconsulting.gwt.resources.client.GWTUtils;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.layout.HorizontalLayout;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.*;
import com.gwtext.client.core.EventObject;
import com.ace3l.legal.gwt.domainmodule.gwt.GWTProject;
import com.ace3l.legal.gwt.domainmodule.client.ProjectServiceAsync;
import com.ace3l.legal.gwt.domainmodule.client.ProjectService;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: ageer
 * Date: Jul 25, 2008
 * Time: 11:04:14 PM
 * To change this template use File | Settings | File Templates.
 */
public class CreateProjectWidget extends Composite {

    public CreateProjectWidget(final DirectorUnit actionDirector) {
        Panel panel = new Panel();
        panel.setBorder(false);
        panel.setPaddings(15);

        //create the form
        final FormPanel formPanel = new FormPanel();
        formPanel.setFrame(false);
        formPanel.setBorder(false);
        Label lTitle = new Label();
        lTitle.setHtml("Create New Project");
        formPanel.add(lTitle);
        formPanel.setLabelWidth(150);
        formPanel.setPaddings(10,10,10,10);

        final TextField projectName = new TextField("Project Name", "projectname", 600);
        projectName.setAllowBlank(false);
        projectName.setMaxLength(30);
        projectName.setMinLength(4);

        formPanel.add(projectName);

        //final RichTextArea rta = new RichTextArea();
//        final HtmlEditor projectDesc = new HtmlEditor();
        final RichTextField projectDesc = new RichTextField();
        projectDesc.setFieldLabel("Project Description");
        projectDesc.setName("description");
        projectDesc.setWidth(600);
        projectDesc.setHeight(200);
        formPanel.add(projectDesc);

        final DateField dueDate = new DateField("Due date", "m/d/y");
        dueDate.setAllowBlank(false);
        dueDate.setMinValue(new Date());
        formPanel.add(dueDate);

        final TextField hoursAllotted = new TextField("Hours Allotted", "hours");
        hoursAllotted.setAllowBlank(false);
        hoursAllotted.setMaxLength(4);
        hoursAllotted.setWidth(50);
        hoursAllotted.setRegex("^[1-9][0-9]*$");
        hoursAllotted.setRegexText("Hours must be a valid number.");
        formPanel.add(hoursAllotted);

        ButtonListenerAdapter baCreate = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject eventObject) {
                String sTitle = projectName.getText();
                String sDescription = projectDesc.getValueAsString();
                Date dDueDate = dueDate.getValue();
                Integer iHoursAllotted = Integer.parseInt(hoursAllotted.getText());
                GWTProject myProject = new GWTProject();
                myProject.setTitle(sTitle);
                myProject.setHoursAllotted(iHoursAllotted);
                myProject.setDescription(sDescription);
                myProject.setDueDate(dDueDate);

                AsyncCallback callback = new AsyncCallback() {
                        public void onSuccess(Object result) {
                            actionDirector.applyAction("success");
                        }

                        public void onFailure(Throwable caught) {
                            actionDirector.applyAction("failure");
                        }
                    };
                ProjectServiceAsync projectService = ProjectService.App.getInstance();
                projectService.createProject(myProject, GWTUtils.getUserId(), callback);
            }
        };

        ButtonListenerAdapter baCancel = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject eventObject) {
                actionDirector.applyAction("cancel");   
            }
        };

        final Button buttonCreate = new Button("Create Project", baCreate);
        final Button buttonCancel = new Button("Cancel", baCancel);

        Panel buttonPanel = new Panel();
        buttonPanel.setLayout(new HorizontalLayout(10));
        buttonPanel.add(buttonCancel);
        buttonPanel.add(buttonCreate);

        formPanel.add(buttonPanel);
        panel.add(formPanel);

        initWidget(formPanel);
    }

}
